package com.jiawa.train.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiawa.train.business.domain.po.TrainStation;
import com.jiawa.train.business.mapper.TrainStationMapper;
import com.jiawa.train.business.req.TrainStationQueryReq;
import com.jiawa.train.business.req.TrainStationSaveReq;
import com.jiawa.train.business.resp.TrainStationQueryResp;
import com.jiawa.train.business.service.ITrainStationService;
import com.jiawa.train.common.exception.BusinessException;
import com.jiawa.train.common.exception.BusinessExceptionEnum;
import com.jiawa.train.common.resp.PageResp;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 火车车站 服务实现类
 * </p>
 *
 * @author hrz
 * @since 2025-09-07
 */
@Service
@RequiredArgsConstructor
public class TrainStationServiceImpl extends ServiceImpl<TrainStationMapper, TrainStation> implements ITrainStationService {

    @Override
    public void saveTrainStation(TrainStationSaveReq req) {
        //1.判断是新增还是更新
        if(ObjectUtil.isEmpty(req.getId())){
            //新增
            //2.先根据三个字段查询是否重复
            TrainStation one = this.lambdaQuery()
                    .eq(req.getTrainCode()!=null,TrainStation::getTrainCode, req.getTrainCode())
                    .eq(req.getIndex()!=null,TrainStation::getIndex, req.getIndex())
                    .eq( req.getName()!=null,TrainStation::getName, req.getName())
                    .one();
            if(ObjectUtil.isNotEmpty(one)){
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_STATION_NAME_UNIQUE_ERROR);
            }
            TrainStation trainStation = BeanUtil.copyProperties(req, TrainStation.class);
            trainStation.setCreateTime(LocalDateTime.now());
            trainStation.setUpdateTime(LocalDateTime.now());
            this.save(trainStation);
        }else{
            //更新
            TrainStation trainStation = this.getById(req.getId());
            BeanUtil.copyProperties(req, trainStation);
            trainStation.setUpdateTime(LocalDateTime.now());
            this.updateById(trainStation);
        }
    }

    @Override
    public void delete(Long id) {
        if(ObjectUtil.isEmpty(this.getById(id))){
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        }
        this.removeById(id);
    }

    @Override
    public PageResp<TrainStationQueryResp> queryList(TrainStationQueryReq req) {
        //1.取得车次编号
        String trainCode = req.getTrainCode();
        //2分页查询
        Page<TrainStation> page = this.lambdaQuery()
                .eq(TrainStation::getTrainCode, trainCode)
                .orderByDesc(TrainStation::getIndex)
                .page(new Page<>(req.getPage(), req.getSize()));
        List<TrainStation> records = page.getRecords();
        if(ObjectUtil.isEmpty(records)){
            PageResp<TrainStationQueryResp> objectPageResp = new PageResp<>();
            objectPageResp.setTotal(0L);
            objectPageResp.setList(new ArrayList<>());
            return objectPageResp;
        }
        //3.转VO返回
        List<TrainStationQueryResp> list = BeanUtil.copyToList(records, TrainStationQueryResp.class);
        PageResp<TrainStationQueryResp> objectPageResp = new PageResp<>();
        objectPageResp.setTotal(page.getTotal());
        objectPageResp.setList(list);
        return objectPageResp;

    }


}
